package org.lilred.nzbberry.models;

import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.lilred.nzbberry.webservice.SabnzbdActionClient;

public class QueueNzbModel {
	private String id;
	private String filename;
	private String size;
	private String mbleft;
	private String cat;
	private String mbtotal;
	private String eta;
	private String timeleft;
	private String status;
	private int index;
	
	public QueueNzbModel(JSONObject slotJson) throws JSONException {
		update(slotJson);
	}
	
	public void update(JSONObject slotJson) throws JSONException {
		id = slotJson.getString("nzo_id");
		filename = slotJson.getString("filename");
		size = slotJson.getString("size");
		mbtotal = slotJson.getString("mb");
		mbleft = slotJson.getString("mbleft");
		cat = slotJson.getString("cat");
		index = slotJson.getInt("index");
		eta = slotJson.getString("eta");
		status = slotJson.getString("status");
		timeleft = slotJson.getString("timeleft");
	}
	
	public String getId() { return id; }
	public String getFilename() { return filename; }
	public String getSize() { return size; }
	public String getMbTotal() { return mbtotal; }
	public String getMbLeft() { return mbleft; }
	public String getCat() { return cat; }
	public double getPercentage() { return (Double.parseDouble(mbtotal) - Double.parseDouble(mbleft)) / Double.parseDouble(mbtotal); }
	public int getIndex() { return index; }
	public void setIndex(int index) { this.index = index; }
	public String getETA() { return eta; }
	public boolean isPaused() { return status.compareTo("Paused") == 0; }
	public String getTimeleft() { return timeleft; }
	public String getStatus() { return status; }
	
	public void pause() {
		SabnzbdActionClient pauseClient = new SabnzbdActionClient();
		pauseClient.pause(id);
		status = "Paused";
	}

	public void resume() {
		SabnzbdActionClient resumeClient = new SabnzbdActionClient();
		resumeClient.resumeOne(id);
		status = "Queued";
	}

	public void moveDown() {
		SabnzbdActionClient moveDownClient = new SabnzbdActionClient();
		index = index + 1;
		moveDownClient.move(id, index);
	}

	public void moveUp() {
		SabnzbdActionClient moveUpClient = new SabnzbdActionClient();
		index = index - 1;
		moveUpClient.move(id, index);
	}

	public void delete() {
		SabnzbdActionClient deleteClient = new SabnzbdActionClient();
		deleteClient.delete(id);
	}
}
